var menuRoot;
var manageTabs;
$(function() {

	menuRoot = $("#manage_menu_root");
	manageTabs = $("#manage_tabs");

	$.post("manage/listMenuGroup.html", function(data) {
		if (data) {
			menuRoot.accordion({
				fit : true,
				border : false,
				onSelect : showMenuItemList
			});
			$.each(data, function(inx, value) {
				var isSelected = false;
				if (inx == 0)
					isSelected = true;
				addMenuGroup(value.id, value.name, isSelected);
			});
		}
	}, "json");

});

// 增加一个目录组
function addMenuGroup(id, name, isSelected) {
	menuRoot.accordion("add", {
		id : id,
		title : name,
		selected : isSelected,
		iconCls : "icon-item"
	});
}

// 初始化不同目录组中的具体目录项
function showMenuItemList(menuGroupName, menuGroupIndex) {
	var menuGroup = menuRoot.accordion("getPanel", menuGroupIndex);
	var menuGroupId = menuGroup.attr("id");
	menuGroup.append("<table id='tb_" + menuGroupId + "'></table>");
	$("#tb_" + menuGroupId).datagrid({
		url : "manage/listChildrenMenu.html",
		queryParams : {
			"groupId" : menuGroupId
		},
		fit : true,
		fitColumns : true,
		nowrap : false,
		border : false,
		idField : "id",
		singleSelect : true,
		showHeader : false,
		onSelect : function(rowIndex, rowData) {
			var tabInx = -1;
			var tabs = manageTabs.tabs("tabs");
			$.each(tabs, function(index, data) {
				if (data.attr("id") == rowData.id) {
					tabInx = manageTabs.tabs("getTabIndex", data);
					return false;
				}
			});
			if (tabInx != undefined && tabInx > -1) {
				manageTabs.tabs("select", tabInx);
			} else {
				manageTabs.tabs("add", {
					id : rowData.id,
					title : rowData.name,
					closable : true,
					href : rowData.url,
					onDestroy : function(){
						dataTable = undefined;
						listUrl = undefined;
						saveUrl = undefined;
						removeUrl = undefined;

						editingRowIndex = undefined;
						isSelectedAll = undefined;
					}
				});
			}
		},
		rowStyler : function(index, row) {
			return "cursor:pointer;";
		},
		columns : [ [ {
			field : "name",
			width : 100,
			styler : function(value, row, index) {
				return 'border:0px';
			}

		} ] ]
	});
}